package com.zoro.barn.business.service.pp.business.product.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zoro.barn.business.service.pp.business.product.dto.BarnProductDto;
import com.zoro.barn.business.service.pp.business.product.entity.BarnProduct;
import com.zoro.barn.business.service.pp.business.product.entity.BarnProductUser;
import com.zoro.barn.business.service.pp.business.product.service.BarnProductService;
import com.zoro.barn.business.service.pp.server.BarnDocumentTestServerService;
import com.zoro.barn.business.service.pp.server.DocumentSqlTestService;
import com.zoro.barn.commons.foundation.web.PageResultInfoInterface;
import com.zoro.barn.commons.tools.json.JsonUtil;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import com.zoro.barn.dto.documentsql.document.DocumentQueryDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/product")
public class BarnProductController {
    private static final Logger log = LoggerFactory.getLogger(BarnProductController.class);
    private final BarnProductService barnProductService;
    private final BarnDocumentTestServerService barnDocumentTestServerService;
    private final DocumentSqlTestService documentSqlTestService;

    public BarnProductController(BarnProductService barnProductService,
                                 BarnDocumentTestServerService barnDocumentTestServerService, DocumentSqlTestService documentSqlTestService) {
        this.barnProductService = barnProductService;
        this.barnDocumentTestServerService = barnDocumentTestServerService;
        this.documentSqlTestService = documentSqlTestService;
    }

    @RequestMapping("/save")
    public BarnProduct save(@RequestBody BarnProduct data) {
        String pid = WebContentUtil.requestUserInfo().getPid();
        if (StringUtils.isBlank(data.getPid())) {
            data.setCreateId(pid);
            return this.barnProductService.insert(data);
        } else {
            data.setUpdateId(pid);
            return this.barnProductService.update(data);
        }
    }


    @RequestMapping("/query")
    public List<BarnProduct> query(BarnProductUser product) {
        return this.barnProductService.query(product);
    }

    @RequestMapping("/mine")
    public List<BarnProduct> mine(BarnProductUser product) {
        String pid = WebContentUtil.requestUserInfo().getPid();
        product.setUserId(pid);
        return this.barnProductService.query(product);
    }

    @RequestMapping("/getById/{pid}")
    public BarnProductDto getById(@PathVariable("pid") String pid) throws JsonProcessingException {
        BarnProduct product = this.barnProductService.getById(pid);
        BarnProductDto result = JsonUtil.transformType(product, BarnProductDto.class);
        if (result != null) {
            result.setSqlCount(this.barnProductService.countSqlByProduct(pid));
            result.setDocumentCount(this.barnProductService.countDocumentByProduct(pid));
        }

        return result;
    }

    @RequestMapping("test")
    public PageResultInfoInterface<DocumentQueryDto> test() throws Exception {
        DocumentQueryDto param = new DocumentQueryDto();
        param.setCurrent(2);
        param.setPageSize(3);
        Map<String, Object> result = this.documentSqlTestService.getDocumentInfo(param);
        log.info("rest client result: {}", result);
       return barnDocumentTestServerService.getDocumentInfo(param);
    }
}
